﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using CipherLib;
using CipherLib.Cipher;
using CipherLib.Cipher.SMIME;
using CipherLib.Cipher.Xml;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ICipher lib = new CipherEnvelope(this.txt_CN.Text);

            string encrypted = lib.EncryptString(textBox1.Text);

            using (TextWriter sw = File.CreateText("encryptedEnvelope.txt"))
            {
                sw.Write(encrypted);
            }

            MessageBox.Show(encrypted);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ICipher lib = new CipherEnvelope(this.txt_CN.Text);
            string encrypted;

            using (StreamReader sr = File.OpenText("encryptedEnvelope.txt"))
            {
                encrypted = sr.ReadToEnd();
            }
            string decrypted = lib.DecryptString(encrypted);

            MessageBox.Show(decrypted);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CipherXml cXmlDocument = new CipherXml(this.txt_CN.Text);

            XmlDocument encrypted = cXmlDocument.EncryptString(textBox1.Text);

            encrypted.Save("xmlEnvelope.xml");

            MessageBox.Show(encrypted.OuterXml);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.Load("xmlEnvelope.xml");

            CipherXml cXmlDocument = new CipherXml(this.txt_CN.Text);
            string decrypted = cXmlDocument.DecryptString(xmlDocument);

            MessageBox.Show(decrypted);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            CipherXml cXmlDocument = new CipherXml(this.txt_CN.Text);

            XmlDocument encrypted = cXmlDocument.EncryptString(textBox1.Text);
            cXmlDocument.SignDocument(encrypted);

            encrypted.Save("xmlDocument.xml");

            MessageBox.Show(encrypted.OuterXml);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.Load("xmlDocument.xml");

            CipherXml cXmlDocument = new CipherXml(this.txt_CN.Text);
            bool signed = cXmlDocument.VerifySignature(xmlDocument);
            string decrypted = cXmlDocument.DecryptString(xmlDocument);

            MessageBox.Show(signed.ToString() + "//" + decrypted);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {
            ICipher lib = new CipherSMime(this.txt_CN.Text);

            string encrypted = lib.EncryptString(textBox1.Text);

            using (TextWriter sw = File.CreateText("mimeencrypted.txt"))
            {
                sw.Write(encrypted);
            }

            MessageBox.Show(encrypted);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            ICipher lib = new CipherSMime(this.txt_CN.Text);
            string encrypted;

            using (StreamReader sr = File.OpenText("mimeencrypted.txt"))
            {
                Encoding en = sr.CurrentEncoding;
                encrypted = sr.ReadToEnd();
            }



            string decrypted = lib.DecryptString(encrypted);

            MessageBox.Show(decrypted);
        }

        private void button9_Click(object sender, EventArgs e)
        {
  
        }

        private void button10_Click(object sender, EventArgs e)
        {
  
        }
    }
}